package com.nice.nice201110.service;

import com.nice.nice201110.entity.TLog;
import com.nice.nice201110.entity.TRole;
import com.nice.nice201110.mapper.TLogMapper;
import com.nice.nice201110.mapper.TRoleMapper;
import com.nice.nice201110.utils.NiceTableEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TroleService {
    @Autowired
    TRoleMapper tRoleMapper;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private TLog tLog = new TLog();

    /** 查询数据 */
    public List<TRole> getList(BaseVo baseVo){
        return tRoleMapper.selectByPrimaryKeys(baseVo);
    }
    /** 单一查询  */
    public TRole getOne(UserVo userVo, Integer id){
        TRole tRole = tRoleMapper.selectByPrimaryKey(id);
        if(tRole!=null){
            tLog.setDate(new Date());
            tLog.setType(3);  //操作类型（新增0，修改1，删除2,查询3）
            tLog.setDocid(tRole.getId());
            tLog.setUsername(userVo.getName());
            tLog.setUserid(Integer.valueOf(userVo.getId()));
            tLog.setTtable(NiceTableEnum.form_role);
            tLog.setRemark("查看角色信息:"+tRole.toString());
            tLogMapper.insertSelective(tLog);
        }
        return tRole;
    }
    /** 插入信息 */
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Boolean insertTrole(UserVo userVo,TRole tRole){
        try {
            int num = tRoleMapper.insertSelective(tRole);
            if(num>0){
                tLog.setDate(new Date());
                tLog.setType(0);  //操作类型（新增0，修改1，删除2）
                tLog.setDocid(tRole.getId());
                tLog.setUsername(userVo.getName());
                tLog.setUserid(Integer.valueOf(userVo.getId()));
                tLog.setTtable(NiceTableEnum.form_role);
                tLog.setRemark("新增角色信息:"+tRoleMapper.toString());
                tLogMapper.insertSelective(tLog);
            }else{
                return false;
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;

    }
}
